@include('admin.layout.bone_header')

<div class="layui-btn-container">

    <button class="layui-btn" data-type="addImages">添加商品</button>

</div>

<div class="layui-form layui-form-pane search">

    <div class="layui-form-item">



        <label class="layui-form-label">商品搜索</label>
        <div class="layui-input-inline">
            <input type="text" id="title_like" autocomplete="off" placeholder="请输入标题搜索" class="layui-input">
        </div>

        <label class="layui-form-label">分类搜索</label>
        <div class="layui-input-inline">
            <select id="category_id" lay-verify="">
                <option value="" selected>全部</option>
                @foreach($categorys as $value)
                    <option value="{{$value['category_id']}}">{{$value['category_name']}}</option>
                @endforeach
            </select>
        </div>




        <button class="layui-btn" data-type="reload">搜索</button>
    </div>
</div>


<table class="layui-hide" id="think-table" lay-filter="think-table"></table>

<script type="text/html" id="barDemo">

    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="del">删除</a>


</script>

<script>

    layui.use('table', function () {
        let table = layui.table;
        UI.getPageTable(table, "{{route('admin.goods.goods')}}", [
            {field: 'goods_id', title: 'ID', width: 80, sort: true, fixed: 'left'}
            , {field: 'goods_name', title: ' 商品名称'}
            , {field: 'goods_price', title: ' 商品价格'}
            , {field: 'category_name', title: ' 分类名称',templet:function (d){

                    return d['category_info']['category_name']
                }}
            , {field: 'mix_goods_price', title: ' 分红值'}
            , {field: 'max_recive', title: ' 最大收益'}
            , {field: 'send_vip_text', title: ' 是否赠送会员',templet:function (d){
                    if(d['send_vip'] == 1){
                        return '是';
                    }else if(d['send_vip'] == 0){
                        return '否';
                    }
                    return d['send_vip']
                }}
            , {field: 'show', title: ' 上下架',templet:function (d){
                    if(d['show'] == 1){
                        return '隐藏';
                    }else if(d['show'] == 0){
                        return '显示';
                    }
                    return d['send_vip']
                }}
            , {field: 'created_at', title: ' 创建时间',templet:function (d){

                    return Utils.paraseTime(d['created_at'])
                }}
            ,{fixed: 'right',title:'操作', align:'center', toolbar: '#barDemo'}
        ]);

        table.on('tool(think-table)', function (obj) {
            let data = obj.data;
            if(obj.event === 'edit'){
                UI.openLayer("{{route('admin.goods.edit')}}?goods_id="+data['goods_id'],980);
            }else if(obj.event == 'del'){
                UI.delete(data['goods_id'],function(res){
                    table.reload('think-table');
                },"{{route('admin.goods.del')}}");
            }
        });
        let $ = layui.$, active = {
            addImages:function (){
                UI.openLayer("{{route('admin.goods.add')}}",980);
            },
            reload:function (){
                let title_like = $('#title_like');
                let category_id = $('#category_id');
                //执行重载
                table.reload('think-table', {
                    page: {
                        curr: 1 //重新从第 1 页开始
                    }
                    ,where: {
                        'title_like':title_like.val(),
                        'category_id':category_id.val(),
                    }
                });
            }
        };
        $('.layui-btn-container .layui-btn').on('click', function () {
            let type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

        $('.search .layui-btn').on('click', function () {
            let type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

    })


</script>


@include('admin.layout.bone_footer')
